package com.fxw.mylibrary.util;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

@SuppressWarnings("all")
public class AndroidUtil {

	public static final String SDCARD_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
	private static String cache_dir = null;

	/**
	 * 拨打电话
	 *
	 * @param context
	 * @param phoneNum
	 */
	public static void callPhone(Context context, String phoneNum) {
		Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
		context.startActivity(intent);
	}

	/**
	 * 获取手机Id
	 *
	 * @param context
	 * @return
	 */
	public static String PeopleDeviceId(Context context) {
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceid = tm.getDeviceId();
		return deviceid;
	}

	/**
	 * 获取手机型号
	 *
	 * @return
	 */
	public static String PeopleModel() {
		Build bd = new Build();
		String model = bd.MODEL;
		return model;
	};

	/**
	 * 隐藏输入法
	 *
	 * @param activity
	 * @param v
	 */
	public void hiddenSoftInputmethod(Activity activity, View v) {
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (v == null) {
			v = activity.getWindow().getCurrentFocus();
		}
		if (v != null) {
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		}
	}

	/**
	 * 显示输入法
	 *
	 * @param activity
	 * @param v
	 */
	public void visibleSoftInputmethod(Activity activity, View v) {
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (!imm.isActive()) {
			imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
		}
		if (v == null) {
			v = activity.getWindow().getCurrentFocus();
		}
		if (v != null) {
			imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
		}
	}

	/**
	 * 获取版本名称
	 *
	 * @param context
	 * @return
	 */
	public static String getVersionName(Context context) {
		String version = null;
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
			version = packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return version;
	}

	/**
	 * 获取包名
	 * 
	 * @param context
	 * @return
	 */
	public static String getPkgName(Context context) {
		String pkgName = null;
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
			pkgName = packageInfo.packageName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return pkgName;
	}

	// 获取ApiKey
	public static String getMetaValue(Context context, String metaKey) {
		Bundle metaData = null;
		String value = null;
		if (context == null || metaKey == null) {
			return null;
		}
		try {
			ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
			if (null != ai) {
				metaData = ai.metaData;
			}
			if (null != metaData) {
				value = metaData.getString(metaKey);
			}
		} catch (NameNotFoundException e) {

		}
		return value;
	}


}
